package com.aisilesn.base.controller;

import com.aisilesn.base.pojo.Label;
import com.aisilesn.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Mr.liu
 * @Description:
 * @Date: Create in 8:50 2019/4/19
 */
@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController {

    @Autowired
    LabelService labelService;

    /**
     * 添加标签
     * @param label
     * @return
     */
    @RequestMapping(method= RequestMethod.POST)
        public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 查询全部标签
     * @param
     * @return
     */
    @RequestMapping(method= RequestMethod.GET)
        public Result findAll(){
        List<Label> all = labelService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",all);
    }

    /**
     * 根据ID查询
     * @param labelId,标签ID
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result fingById(@PathVariable(value = "labelId") String labelId){
        Label byId = labelService.findById(labelId);
        return new Result(true,StatusCode.OK,"查询成功",byId);
    }

    /**
     * 修改
     * @param labelId
     * @param label
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result findById(@PathVariable(value = "labelId") String labelId,@RequestBody Label label){
        label.setId(labelId);
        labelService.update(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable(value = "labelId") String labelId){
        labelService.deleteById(labelId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap){
        List<Label> search = labelService.findSearch(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",search);
    }

    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "search/{page}/{size}",method = RequestMethod.POST)
    public Result findSearchPage(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Label> search = labelService.findSearch(searchMap,(page-1),size);
        PageResult<Label> labelPageResult = new PageResult<>(search.getTotalElements(), search.getContent());
        return new Result(true,StatusCode.OK,"查询成功",labelPageResult);
    }

}
